// 
//  Copyright (C) 2004-2009  Autodesk, Inc.
//  
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of version 2.1 of the GNU Lesser
//  General Public License as published by the Free Software Foundation.
//  
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//  
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#ifndef _MAPINFOINSERT_H_
#define _MAPINFOINSERT_H_

#include "MapInfoFeatureCommand.h"

class MapInfoInsert : public MapInfoCommand<FdoIInsert>
{
    public:
        MAPINFO_API MapInfoInsert(MapInfoConnection* connection) : MapInfoCommand<FdoIInsert>(connection)
        {
            m_className = NULL;
            m_properties = FdoPropertyValueCollection::Create();
        }

    protected:
        MAPINFO_API virtual ~MapInfoInsert()
        {
            FDO_SAFE_RELEASE(m_className);
            FDO_SAFE_RELEASE(m_properties);
        }

    //-------------------------------------------------------
    // FdoIInsert implementation
    //-------------------------------------------------------

    public:
        MAPINFO_API virtual FdoIdentifier* GetFeatureClassName() { return FDO_SAFE_ADDREF(m_className); }
        MAPINFO_API virtual void SetFeatureClassName(FdoIdentifier* value)
        {
            FDO_SAFE_RELEASE(m_className);
            m_className = FDO_SAFE_ADDREF(value);
        }
        MAPINFO_API virtual void SetFeatureClassName(FdoString* value)
        {
            FDO_SAFE_RELEASE(m_className);
            m_className = NULL;
            if (value)
                m_className = FdoIdentifier::Create(value);
        }
        MAPINFO_API virtual FdoPropertyValueCollection* GetPropertyValues() { return FDO_SAFE_ADDREF(m_properties); }
        MAPINFO_API virtual FdoBatchParameterValueCollection* GetBatchParameterValues()
        {
            if (m_batchParameters.p == NULL)
                m_batchParameters = FdoBatchParameterValueCollection::Create();

            return (FDO_SAFE_ADDREF(m_batchParameters.p));
        }
        MAPINFO_API virtual FdoIFeatureReader* Execute()
        {
            // verify connection
            if (m_connection == NULL)
                throw FdoCommandException::Create(NlsMsgGetMain(FDO_NLSID(MAPINFOPROVIDER_NO_CONNECTION)));

            if ((m_batchParameters.p == NULL) || (m_batchParameters->GetCount()==0))
                return m_connection->Insert(m_className, m_properties);
            else
                return m_connection->Insert(m_className, m_batchParameters, m_properties);
        }

    private:
        FdoIdentifier* m_className;
        FdoPropertyValueCollection* m_properties;
        FdoPtr<FdoBatchParameterValueCollection> m_batchParameters;
};

#endif

